"""
Power api
"""


import json

from .command import raw_command
from .argument import build_args
from redfishtoollib import main


# restart server
def restart():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Systems', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'GET', base_url, None)
        res = main(cmd)
        try:
            state = json.loads(res)
        except ValueError:
            state = {}
        if state.get('PowerState') == 'On':
            cmd = raw_command(args, 'POST', base_url + '/Actions/ComputerSystem.Reset', '{"ResetType": "ForceRestart"}')
            res = main(cmd)
        elif state.get('PowerState') == 'Off':
            cmd = raw_command(args, 'POST', base_url + '/Actions/ComputerSystem.Reset', '{"ResetType": "On"}')
            res = main(cmd)
        else:
            cmd = raw_command(args, 'POST', base_url + '/Actions/ComputerSystem.Reset', '{"ResetType": "ForceRestart"}')
            res = main(cmd)
        try:
            restart = json.loads(res)
        except ValueError:
            restart = {}
        if ('SUCCESS' in restart.get('error', {}).get('message', '').upper()) or ('SUCCESS' in restart.get('error', {}).get('Message', '').upper()) or ('SUCCESS' in restart.get('error', {}).get('@Message.ExtendedInfo', {})[0].get('Message', '').upper()):
            print('Success')


# start server
def start():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Systems', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'POST', base_url + '/Actions/ComputerSystem.Reset', '{"ResetType": "On"}')
        res = main(cmd)
        try:
            start = json.loads(res)
        except ValueError:
            start = {}
        if ('SUCCESS' in start.get('error', {}).get('message', '').upper()) or ('SUCCESS' in start.get('error', {}).get('Message', '').upper()) or ('SUCCESS' in start.get('error', {}).get('@Message.ExtendedInfo', {})[0].get('Message', '').upper()):
            print('Success')


# stop server
def stop():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Systems', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'POST', base_url + '/Actions/ComputerSystem.Reset', '{"ResetType": "ForceOff"}')
        res = main(cmd)
        try:
            stop = json.loads(res)
        except ValueError:
            stop = {}
        if ('SUCCESS' in stop.get('error', {}).get('message', '').upper()) or ('SUCCESS' in stop.get('error', {}).get('Message', '').upper()) or ('SUCCESS' in stop.get('error', {}).get('@Message.ExtendedInfo', {})[0].get('Message', '').upper()):
            print('Success')


# server power status
def status():
    args = build_args()
    cmd = raw_command(args, 'GET', '/redfish/v1/Systems', None)
    res = main(cmd)
    try:
        urls = json.loads(res)
    except ValueError:
        urls = {}
    if len(urls.get('Members', [])) > 0:
        base_url = urls.get('Members', [])[0].get('@odata.id')
        cmd = raw_command(args, 'GET', base_url, None)
        res = main(cmd)
        try:
            state = json.loads(res)
        except ValueError:
            state = {}
        print(state.get('PowerState'))
